package com.fly.okhttpproject;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.fly.okhttp.base.BaseOKHttpResultCallBack;
import com.fly.okhttp.util.OKHttpUtil;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.id_12306_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    OKHttpUtil.setCertificates(getAssets().open("srca.cer"));
                    OKHttpUtil.getAsync("https://kyfw.12306.cn/otn/", new BaseOKHttpResultCallBack() {
                        @Override
                        public void noInternet() {
                            Log.e("tag", "没有网络");
                        }

                        @Override
                        public void onFailure(Exception e) {
                            Log.e("tag", "失败 " + e.getMessage());
                        }

                        @Override
                        public void onResponse(String result) {
                            Log.e("tag", result);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        findViewById(R.id.id_get_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OKHttpUtil.getAsync(this, "http://www.baidu.com", new BaseOKHttpResultCallBack() {
                    @Override
                    public void noInternet() {
                        Log.e("tag", "没有网络");
                    }

                    @Override
                    public void onFailure(Exception e) {
                        Log.e("tag", "失败 " + e.getMessage());
                    }

                    @Override
                    public void onResponse(String result) {
                        Log.e("tag", result);
                    }
                });
            }
        });
        findViewById(R.id.id_download_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OKHttpUtil.downLoadFileGetAsync(MainActivity.this,"http://d.hiphotos.baidu.com/zhidao/pic/item/ca1349540923dd5425d2bcccd009b3de9d8248f4.jpg",
                        Environment.getExternalStorageDirectory().getAbsolutePath(),
                        "wolf.jpg",
                        new BaseOKHttpResultCallBack() {
                            @Override
                            public void noInternet() {
                                Log.e("tag", "没有网络");
                            }

                            @Override
                            public void onFailure(Exception e) {
                                Log.e("tag", "失败 " + e.getMessage());
                            }

                            @Override
                            public void onResponse(String result) {
                                Log.e("tag", result);
                            }

                            @Override
                            public void onDownProgress(int len, long currentTotalLen, long total) {
                                Log.e("tag", len + "-" + currentTotalLen + "-" + total);
                            }
                        });
            }
        });

/*
        OKHttpParams params = new OKHttpParams();
        ArrayList<File> files = new ArrayList<>();
//        files.add();

        params.put("userImageFile", files);
        OKHttpUtil.uploadFile("", "image*/
/*", params, new BaseOKHttpResultCallBack() {
            @Override
            public void onResponse(String result) {
                Log.e("tag", result);
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("tag", "失败");
            }

            @Override
            public void noInternet() {
                Log.e("tag", "没有网络");
            }
        });
*/

        findViewById(R.id.id_cancel_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OKHttpUtil.cancelRequest(MainActivity.this, new OKHttpUtil.OnCancelRequestHandler() {
                    @Override
                    public void onCancle() {
                        Log.e("tag", "取消请求");
                    }
                });
            }
        });

        findViewById(R.id.id_cancel_all_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                OKHttpUtil.cancelAll(new OKHttpUtil.OnCancelRequestHandler() {
                    @Override
                    public void onCancle() {
                        Log.e("tag", "取消全部");
                    }
                });
            }
        });
    }

}
